package com.shoulder.boot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shoulder.boot.system.dto.DictAddDTO;
import com.shoulder.boot.system.dto.DictModDTO;
import com.shoulder.boot.system.dto.DictQueryDTO;
import com.shoulder.boot.system.entity.Dict;
import com.shoulder.boot.system.exception.SystemException;
import com.shoulder.boot.system.mapper.DictMapper;
import com.shoulder.boot.system.service.DictService;
import com.shoulder.boot.system.utils.DictUtils;
import com.shoulder.boot.system.vo.DictVo;
import com.shoulder.core.fetch.BaseIds;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 字典信息 服务实现类
 *
 * @author unKnown
 * @since 2025-08-28 10:33:40
 */
@Service
@Transactional
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {


    @Override
    public List<DictVo> listQuery(DictQueryDTO queryDTO) {
        List<Dict> list = this.baseMapper.selectList(DictUtils.buildQueryWrapper(queryDTO));
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return DictUtils.buildUtils().toVos(list);
    }

    @Override
    public DictVo look(String id) {
        Dict entity = this.baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            throw new SystemException("字典信息不存在");
        }
        return DictUtils.buildUtils().toVo(entity);
    }

    @Override
    public List<DictVo> looks(BaseIds<String> baseIds) {
        List<Dict> list = this.baseMapper.selectBatchIds(baseIds.getIds());
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return DictUtils.buildUtils().toVos(list);
    }

    @Override
    public void del(String id) {
        Dict entity = this.baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            throw new SystemException("字典信息不存在");
        }
        this.baseMapper.deleteById(id);
    }

    @Override
    public void batchDel(BaseIds<String> baseIds) {
        this.baseMapper.deleteBatchIds(baseIds.getIds());
    }


    @Override
    public Page<DictVo> pageQuery(DictQueryDTO queryDTO) {
        Page<Dict> selectPage = this.baseMapper.selectPage(new Page<>(queryDTO.getCurrent(), queryDTO.getSize())
                , DictUtils.buildQueryWrapper(queryDTO));
        if (selectPage == null || CollectionUtils.isEmpty(selectPage.getRecords())) {
            return new Page<>();
        }
        return DictUtils.buildUtils().toPage(selectPage);
    }

    @Override
    public void mod(DictModDTO modDTO) {
        Dict info = Optional.ofNullable(this.baseMapper.selectById(modDTO.getId())).orElseThrow(() -> new SystemException("字典信息不存在"));
        Dict dict = this.baseMapper.selectOne(new LambdaQueryWrapper<Dict>().eq(Dict::getName, modDTO.getType()).eq(Dict::getValue, modDTO.getValue())
                .ne(Dict::getId, info.getId()));

        if (dict != null) {
            throw new SystemException("字典数据已存在");
        }
        BeanUtils.copyProperties(modDTO, info);
        this.baseMapper.updateById(info);
    }

    @Override
    public void add(DictAddDTO addDTO) {

        Dict dict = this.baseMapper.selectOne(new LambdaQueryWrapper<Dict>().eq(Dict::getType, addDTO.getType()).eq(Dict::getValue, addDTO.getValue()));
        if (dict != null) {
            throw new SystemException("字典数据已存在");
        }
        Dict info = new Dict();
        BeanUtils.copyProperties(addDTO, info);
        this.baseMapper.insert(info);
    }
}